﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace ZCSBZPJ.Models.ZP.Common
{
    public class FileHelper
    {
        public static string GetFileBase64ByImg(HttpPostedFileBase oFile,out string errorMsg)
        {
            errorMsg = "";
            string strBaser64 = "";
            string extName = System.IO.Path.GetExtension(oFile.FileName); //得到扩展名
            using (Stream inputStream = oFile.InputStream)
            {
                MemoryStream ms = inputStream as MemoryStream;
                if (ms == null)
                {
                    ms = new MemoryStream();
                    inputStream.CopyTo(ms);
                }
                /*Bitmap bmp = new Bitmap(oFile.FileName);
                ImageFormat fmt = null;
                switch (extName.ToLower())
                {
                    case "gif":
                        fmt = ImageFormat.Gif;
                        break;
                    case "jpeg":
                        fmt = ImageFormat.Jpeg;
                        break;
                    case "png":
                        fmt = ImageFormat.Png;
                        break;
                    case "bmp":
                        fmt = ImageFormat.Bmp;
                        break;
                    case "emf":
                        fmt = ImageFormat.Emf;
                        break;
                    case "exif":
                        fmt = ImageFormat.Exif;
                        break;
                    case "icon":
                        fmt = ImageFormat.Icon;
                        break;
                    case "memorybmp":
                        fmt = ImageFormat.MemoryBmp;
                        break;
                    case "tiff":
                        fmt = ImageFormat.Tiff;
                        break;
                    case "wmf":
                        fmt = ImageFormat.Wmf;
                        break;
                    default:
                        errorMsg = string.Format("文件类型不正确，没有找图片类型为'{0}'", extName);
                        break;
                }
                if (string.IsNullOrWhiteSpace(errorMsg))
                {
                    bmp.Save(ms, fmt);
                    byte[] arr = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(arr, 0, (int)ms.Length);
                    ms.Close();
                    strBaser64 = Convert.ToBase64String(arr);
                }*/

                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                strBaser64 = Convert.ToBase64String(arr);
            }
            return strBaser64;
        }
    }
}
